<?php
namespace Home\Controller;
use Think\Controller;
class DetailsController extends Controller {
    public function index(){
    	$id=$_GET['id'];
    	if (empty($_GET)) {
    		//如果商品ID为空则直接跳到首页
    		$this->redirect('index/index');
    		return false;
    	}
    	/*----------------------遍历商品的评论-----------------------------*/
	    //连表查询评论
	    $comme = M('arguments');
			$comm = $comme ->field('arg.text argtext,arg.addtime argadd,u.name uname')->table('arguments arg,user u')->where('arg.uid=u.id and arg.status=1 and arg.goods_id='.$id)->select();
				//var_dump($comm);
			$this->assign('comm',$comm);
		/*------------------------------------------------------------------*/

    	$model= D ('Goods');
    	$Details=$model->setAll($id);
    	//------遍历详情图--------------------------------------
    	$model_img=M('goods_img');
    	$goods_list=$model_img->where('goods_id='.$id)->select();
    	$isface1=[];//详情图
    	$isface2=[];//封面图
    	
		foreach ($goods_list as  &$value) {
			if ($value['is_face']==0) {
				$isface1[]=$value;
			}
			if ($value['is_face']==1) {
				$isface2[]=$value;
			}
		}
		$isface3=$isface1;//封面图片少把详情借来用
		//详情图拼接地址
		foreach ($isface1 as $ke => $val) {
			$isface1[$ke]['name']='/public/Uploads/'.$this->substr($val['name']).'109_'.$val['name'];
		}

		//封面图地址
		foreach ($isface2 as $k => $va) {
			$isface2[$k]['name']='/public/Uploads/'.$this->substr($va['name']).'768_'.$va['name'];
		}

		//把详情的图片也截取2张放到封面去
		$isface4=array_slice($isface3,0,2);
		//把详情图片当作封面JS滚动效果
		foreach ($isface4 as $kk => $vav) {
			$isface4[$kk]['name']='/public/Uploads/'.$this->substr($vav['name']).'768_'.$vav['name'];
		}
		// var_dump($isface4);
		
    	$this->assign('isface1',$isface1);
    	$this->assign('isface2',$isface2);
    	$this->assign('isface4',$isface4);
    	//------------------------------------------------------
    	// var_dump($isface1);
    	//商品
    	$this->assign('Details',$Details);

    	$this->display();
    }

    //拼接文件上传子目录  接收图片名
	 private function substr($filename)
	 {
	 	return substr($filename,0,4).'/'.substr($filename,4,2).'/'.substr($filename,6,2).'/';
	 }

//*-----------------商品评论-------------------------------
	public function comments(){

		$data['text'] = $_POST['text'];
		$data['uid'] = $_SESSION['user']['id'];
		$data['goods_id'] =I('get.id');
		$data['order_id'] =I('post.orl_id');
		$data['addtime'] = time();
		//插入数据库
		$model = M('arguments');

				if (empty($data['uid'])) 
				{
					$this->error('请先登录');
					return false;
				}
				if (empty($data['order_id'])) 
				{
					$this->error('请先购买商品');
					return false;
				}
				foreach ($data as $key => $value) {
					if (empty($value)) {
						$this->error('请添加评论');
						return false;
					}
				}
		
		//var_dump($data['order_id']);
		$list = $model->data($data)->add();
		if ($list) {
			
			$this->redirect('Details/index');
		}
	}

/*-------------------------商品评论完----------------------------------*/


 }